<template>
    <div class="recyclebin">
        <el-input clearable v-model="sreach.keyword" style="width: 185px;" placeholder="输入任务名称/姓名查询" size="small" />
        <el-select v-model="sreach.taskType" placeholder="请选择任务类型" size="small" clearable>
            <el-option label="限时任务" value="1">
            </el-option>
            <el-option label="周期任务" value="2">
            </el-option>
        </el-select>
        <el-select v-model="sreach.taskStatus" placeholder="请选择任务状态" size="small" clearable>
            <el-option label="已延期" value="1">
            </el-option>
            <el-option label="即将到期" value="2">
            </el-option>
            <el-option label="未到期" value="3">
            </el-option>
            <el-option label="已暂停" value="4">
            </el-option>
            <el-option label="已完成" value="5">
            </el-option>
        </el-select>
        <el-select v-model="sreach.fid" placeholder="请选择" size="small">
            <el-option v-for="item in groupsList" :key="item.value" :label="item.name" :value="item.id">
            </el-option>
        </el-select>
        <el-button type="primary" size="small" @click="todoSearch">搜索</el-button>

        <el-table :data="tableData" border style="width: 100%;margin-top: 35px;" :row-class-name="tableRowClassName">
            <el-table-column type="index" label="序号" width="100" align="left" fixed="left">
            </el-table-column>
            <el-table-column prop="taskName" label="任务名称" width="300" align="left">
                <template slot-scope="scope">
                    <i class="el-icon-warning"
                        :class="{ 'red': scope.row.urgentState == 1, 'yellow': scope.row.urgentState == 2, 'green': scope.row.urgentState == 3 }"></i>
                    <span :class="{ 'decoration': scope.row.state == 5 }">{{ scope.row.taskName }}</span>
                </template>
            </el-table-column>
            <el-table-column label="类型">
                <template slot-scope="scope">
                    <span>
                        {{ scope.row.category == 1 ? '限时任务' : '周期任务' }}
                    </span>
                </template>
            </el-table-column>
            <el-table-column label="状态">
                <template slot-scope="scope">
                    <el-tag size="medium" :type="
                        scope.row.state == 1 ? 'danger' :
                            scope.row.state == 2 ? 'warning' :
                                scope.row.state == 3 ? 'success' :
                                    scope.row.state == 4 ? '' :
                                        scope.row.state == 5 ? 'info' : ''">
                        {{
                            scope.row.state == 1 ? '已延期' :
                            scope.row.state == 2 ? '即将到期' :
                                scope.row.state == 3 ? '未到期' :
                                    scope.row.state == 4 ? '已暂停' :
                                        scope.row.state == 5 ? '已完成' : ''
                        }}
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="projectName" label="关联项目" align="left">
            </el-table-column>
            <el-table-column label="截止时间">
                <template slot-scope="scope">
                    {{ scope.row.endDate.substring(0, 16) }}
                </template>
            </el-table-column>
            <el-table-column prop="userName" label="责任人" align="left">
            </el-table-column>
            <el-table-column prop="categoryName" label="其他参与人" align="left">
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="350">
                <template slot-scope="scope">
                    <el-button @click="handleClick(scope.row.id)" type="primary" size="small" plain>查看详情</el-button>
                    <el-button type="danger" size="small" plain v-if="scope.row.deleteShowFlag == 1"
                        @click="recoveryTask(scope.row.id, scope.row.deleteFlag)">恢复</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination style="text-align: right;margin-top: 15px;" @size-change="handleSizeChange"
            @current-change="handleCurrentChange" :current-page="pageObj.currentPage" :page-sizes="[5, 10, 15, 20]"
            :page-size="pageObj.ps" layout="total, sizes, prev, pager, next, jumper" :total="pageObj.total">
        </el-pagination>
        <taskDialog :dialogForm="dialogForm" @cancel="cancel" :taskId="taskId" @submitTask="submitTask"></taskDialog>
    </div>
</template>
<script>
import { getPageList, stopOrDeleteTaskInfo } from "@/service/taskApi"
import {
    selectFunGroupList,
} from "@/service/Api.js"
import taskDialog from "./components/taskDialog.vue"
export default {
    components: { taskDialog },
    data() {
        return {
            sreach: {
                keyword: "",
                taskType: "",
                taskStatus: "",
                fid: "",
            },
            pageObj: {
                pn: 1,
                ps: 10,
                total: 0,
                currentPage: 1,
            },
            tableData: [],
            groupsList: [],
            dialogForm: {
                dialogVisible: false,
                title: '',
                width: '40%'
            },
            taskId: '',
            endDate: "",
        }
    },
    created() {
        
        this.groups()
    },
    methods: {
        tableRowClassName({ row }) {
            if (row.state === 5) {
                return 'completedFlag-row';
            }
            return '';
        },
        submitTask() {
            this.dialogForm.dialogVisible = !this.dialogForm.dialogVisible
            this.getTableData()
        },
        cancel() {
            this.dialogForm.dialogVisible = !this.dialogForm.dialogVisible
        },
        handleClick(val) {
            this.taskId = val
            this.dialogForm.title = '任务详情'
            this.dialogForm.dialogVisible = !this.dialogForm.dialogVisible
        },
        todoSearch() {
            this.pageObj.pn = 1;
            this.getTableData()
        },
        getTableData() {
            let data = {
                pn: this.pageObj.pn,
                ps: this.pageObj.ps,
                category: this.sreach.taskType,
                state: this.sreach.taskStatus,
                keyword: this.sreach.keyword,
                deleteFlag: 1,
                fids : this.sreach.fid,
            }
            getPageList(data).then(res => {
                if (res.code == 0) {
                    this.tableData = res.data.records
                    this.pageObj.pn = res.data.current
                    this.pageObj.ps = res.data.size
                    this.pageObj.total = res.data.total
                    this.pageObj.currentPage = res.data.current
                }
            })
        },
        recoveryTask(id, flag) {
            this.$confirm(`是否恢复此任务?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                stopOrDeleteTaskInfo({ id: id, deleteFlag: 0 }).then(res => {
                    if (res.code == 0) {
                        this.$message({
                            type: 'success',
                            message: `已恢复`
                        });
                        this.getTableData();
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });
        },
        //获取职能组列表
        groups() {
            let userInfo = sessionStorage.getItem("userInfo");
            let id = JSON.parse(userInfo).id;
            selectFunGroupList(id).then((res) => {
                if (res.code == 0) {
                    this.groupsList = res.data;
                    // let f;
                    // let arr = [];
                    // res.data.forEach(item => {
                    //     arr.push(item.id);
                    //     f = arr.join(",")
                    // })
                    let list = [
                        {
                            id: '',
                            name: "所有职能组",
                        },
                    ];
                    list.push(...res.data);
                    this.groupsList = list;
                    if (res.data.length > 0) {
                        let userInfo = sessionStorage.getItem("userInfo");
                        if (JSON.parse(userInfo).fid == 0) {
                            this.sreach.fid = f;
                        } else {
                            this.sreach.fid = JSON.parse(userInfo).fid;
                        }
                    }
                    this.getTableData();
                }
            });
        },
        handleSizeChange(val) {
            if (this.pageObj.total - this.pageObj.ps * this.pageObj.pn < val && this.pageObj.pn > 1) {
                this.pageObj.pn = this.pageObj.pn - 1
            }
            this.pageObj.ps = val
            this.getTableData()
        },
        handleCurrentChange(val) {
            this.pageObj.pn = val
            this.getTableData()
        },
    },
}
</script>
<style>
.red {
    color: red;
}

.yellow {
    color: rgb(255, 179, 0);
}

.green {
    color: green;
}

.el-table .completedFlag-row {
    background: rgb(214, 214, 214);
}

.decoration {
    text-decoration: line-through
}
</style>